<template>
    <mi-form-page @submit="submit" v-loading="edit.loading">
        <el-form :model="edit.info"
                 :rules="rules"
                 ref="formRef">

            <el-form-item prop="package_id" label="套餐">
                <mi-select v-model="edit.info.package_id" :options="packageOptions" />
            </el-form-item>
            <el-form-item prop="title" label="名称">
                <mi-input v-model="edit.info.title"/>
            </el-form-item>
            <el-form-item prop="logo" label="网站logo">
                <mi-upimg v-model="edit.info.logo"/>
            </el-form-item>
            <el-form-item prop="icon" label="网站图标">
                <mi-upimg v-model="edit.info.icon" :size="48" />
            </el-form-item>

            <el-form-item prop="phone" label="客服电话">
                <mi-input v-model="edit.info.phone"/>
            </el-form-item>

            <el-form-item prop="title" label="地址">
                <mi-select-address :info="edit.info" />
            </el-form-item>
            <el-form-item prop="status" label="状态">
                <mi-switch v-model="edit.info.status"/>
            </el-form-item>

        </el-form>
    </mi-form-page>

</template>

<script setup lang="ts">
import {ref, onBeforeMount, inject} from "vue";
import {simpleGet} from "@/utils/request";
import {makeModelAuth} from "@/utils/auth";
import {FormInstance} from "element-plus";
import Edit from "@/model/edit";

const list = inject("list") as any
const edit = inject("edit", new Edit({}))
const auth = makeModelAuth("site.site")
const rules = {
    title:[{required:true, message:'必填'}],
    package_id: [{required:true, message:'必填'}],
}

const formRef = ref<FormInstance>()
const packageOptions = ref<any[]>()

const submit = () => {
    let url = list.currentRow ? auth.edit : auth.add
    edit.submit({
        formRef: formRef.value,
        url: "/" + url,
        list: list,
    })
}

onBeforeMount(() => {
    edit.init(async () => {

        // 套餐
        let res = await simpleGet('/site.sitePackage/options')
        packageOptions.value = res.options

        if (list.currentRow) {
            // 请求表单数据
            let res = await simpleGet('/' + auth.edit, {id: list.currentRow.id})
            edit.info = res.info
        } else {
            edit.info = {
                status: 1,
            }
        }
    })
})

</script>

<style scoped>

</style>
